//
//  OSHomeView.m
//  ZKPayApp
//
//  Created by 凯朱 on 2024/10/29.
//

#import "OSHomeView.h"
#import "OSHomeCell.h"
#import "OSApplyVC.h"
static  NSString *const kCellId = @"OSHomeCell";

@interface OSHomeView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>{
    NSInteger _curIndex;
}

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, strong)UICollectionViewFlowLayout *layout;

@end
@implementation OSHomeView
-(void)awakeFromNib{
    [super awakeFromNib];
    [self.bgView addSubview:self.collectionView];
    _curIndex = 0;
    self.backgroundColor = kHexColor(@"#F8FAFF");
    _sureBtn.layer.cornerRadius = 25;
    _sureBtn.layer.masksToBounds = YES;
    _lbl1.text = kLocalString(@"认证资料");
    _lbl2.text = kLocalString(@"通过审核");
    _lbl3.text = kLocalString(@"立即放款");
    [_sureBtn setTitle:kLocalString(@"立即申请") forState:UIControlStateNormal];
}

- (IBAction)sureAction:(id)sender {
    if (kIsTourst) {
        [OSTool pushToLogVc];
        return;
    }
    OSApplyVC *vc = [OSApplyVC new];
    vc.type = _curIndex+1;
    kWeakSelf(self);
    [vc setApplyBlock:^(NSInteger btnIndex) {
        [weakself resetTabbar];
    }];
    [[OSTool getCurrentUIVC].navigationController pushViewController:vc animated:YES];
}

-(void)resetTabbar{
    OSTabBarController *tab = [OSTabBarController new];
    [UIApplication sharedApplication].keyWindow.rootViewController = tab;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 3;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OSHomeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCellId forIndexPath:indexPath];
    UIView *bgView;
    UILabel *titleLbl;
    UILabel *priceLbl;
    UILabel *tipLbl;
    UIImageView *icon;
    if (indexPath.row == 0) {
        cell.bgView1.hidden = NO;
        cell.bgView2.hidden = YES;
        bgView = cell.bgView1;
        titleLbl = cell.titleLbl1;
        priceLbl = cell.priceLbl1;
        tipLbl = cell.tipLbl1;
        cell.titleLbl1.text = kLocalString(@"消费贷");
        cell.icon1.image = [[UIImage imageNamed:@"gouwudai-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        icon = cell.icon1;
    }else if (indexPath.row == 1 || indexPath.row == 2){
        cell.bgView1.hidden = YES;
        cell.bgView2.hidden = NO;
        bgView = cell.bgView2;
        titleLbl = cell.titleLbl2;
        priceLbl = cell.contenLbl2;
        tipLbl = cell.tipLbl2;
        if (indexPath.row == 1) {
            cell.titleLbl2.text = kLocalString(@"助学贷");
            cell.contenLbl2.text = @"5,000";
            cell.icon2.image = [[UIImage imageNamed:@"xuexi"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        }else{
            cell.titleLbl2.text = kLocalString(@"经营贷");
            cell.contenLbl2.text = @"8,0000";
            cell.icon2.image = [[UIImage imageNamed:@"jingyingdai"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        }
        icon = cell.icon2;
    }

    if (indexPath.row == _curIndex) {
        bgView.backgroundColor = kColor_BG;
        titleLbl.textColor = kColor_W;
        priceLbl.textColor = kColor_W;
        icon.tintColor = kColor_W;
    }else{
        bgView.backgroundColor = kColor_W;
        titleLbl.textColor = kColor_T;
        priceLbl.textColor = kColor_T;
        icon.tintColor = kColor_T;
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_curIndex == indexPath.row) {
        return;
    }
    
    _curIndex = indexPath.row;
    [_collectionView reloadData];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
        return CGSizeMake(164, 230);
  
}

#pragma mark - ================<懒加载>===============
-(UICollectionView *)collectionView{
    if (_collectionView == nil) {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.minimumLineSpacing = 0;
        _layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 230) collectionViewLayout:_layout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = kClearColor;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerNib:[UINib nibWithNibName:kCellId bundle:nil] forCellWithReuseIdentifier:kCellId];
    }
    return _collectionView;
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
@end


